import loading from '@/components/xuan-loading/xuan-loading.vue'
import cLabel from "@/components/cat-label/cat-label.vue"
import fromItem from '@/components/from-item/from-item.vue'
import wx from "weixin-js-sdk";
import QSpicker from '@/components/QuShe-picker/QuShe-picker.vue';
import {
	log
} from 'util'
export default {
	props: {
		mustIcon: {
			type: String,
			default: require('@/static/Scope.png'),
		},
		unIcon: {
			type: String,
			default: require('@/static/unscope.png'),
		}
	},
	data() {
		return {
			Inv: 1,
			positionData: [], //存放位置
			page: false,
			click: '',
			appliance: null,

			newDisabled: false, //基本信息是否可编辑
			newMust: "must", //基本信息必填
			flagNum: -1,
			// check:false,//处理是否可编辑
			gridmanList: [], //所有网格员
			selector: "text",
			date1: "date1",
			// 弹框
			selectParent: true,
			multiple: false,
			companyTree: [], //树结构	
			TreeNum: null, //第几个树结构
			flod: true,

			// loading样式
			width: '150rpx',
			height: '100rpx',
			shadeShow: false,
			backgroundColor: "transparent",
			themeColor: '#33CCCC',
			// loading样式
			formData: {
				fId: "", //
				fWarehousingdates: "", //入库日期
				fPosition: "", //存放位置
				fPositionid: "", //存放位置id
				fWarehousingusername: "", //入库人员
				fWarehousinguserid: "", //入库人员id
				userId: "", //当前登录人id
				userName: "", //当前登录人姓名
				isSubmit: "", //0保存1提交
				fNum: "",
				itemsList: []
			},
			totalData: {}, // 详情

			timeId: null,
			userInfo: null,
			must: "must",
			disabled: false,
			recordfiles: [], //删除文件集合
			optionsF: null, //上个页面传递过来的参数
			timing: null, //当前年月日
			//显示隐藏
			basic: 1, //基本信息
			close: 1, //问题处理情况
			//显示隐藏
			// index: 0,
			multiArray: [
				['亚洲', '欧洲'],
				['中国', '日本'],
				// ['北京', '上海', '广州']
			],
			multiIndex: [0, 0, 0],
			fWorktypeindex: 0,
			postionArr: null,
			layerNum: null,
			ceodeNum: null,
			custom2Set_1: {
				itemArray: [
					[{
						name: "1", //name变量名需与下方steps.steps_1_value相同
						value: "1" //可添加多项自定义想要的数据
					}, {
						name: "2",
						value: "2"
					}, {
						name: "3",
						value: "3"
					}, {
						name: "4",
						value: "4"
					}, {
						name: "5",
						value: "5"
					}],
					[{
						name: "1",
						value: "1"
					}, {
						name: "2",
						value: "2"
					}, {
						name: "3",
						value: "3"
					}, {
						name: "4",
						value: "4"
					}, {
						name: "5",
						value: "5"
					}, {
						name: "6",
						value: "6"
					}, {
						name: "7",
						value: "7"
					}, {
						name: "8",
						value: "8"
					}]
				],
				steps: {
					step_1_value: "name", //第一级显示的属性名
					step_2_value: "", //第二级显示的属性名
					step_3_value: "" //第三级显示的属性名
				},
				defaultValue: [0, 0] //初始数据
			},
		};
	},
	onLoad(options) {

		var that = this;
		uni.getStorage({ //监督检查方式
			key: 'userInfo',
			success: function(res) {
				that.userInfo = JSON.parse(res.data);
				// console.log("that.userInfo", that.userInfo);
				that.formData.fWarehousingusername = that.userInfo.realName //入库人员
				that.formData.fWarehousinguserid = that.userInfo.userId //入库人员id
				that.formData.userName = that.userInfo.realName //入库人员
				that.formData.userId = that.userInfo.userId //入库人员id
				that.optionsF = options;
				that.formData.fId = options.id
				that.getDetailData(options.id)
				that.getPostion(that.userInfo.companyId)
			}
		});



		// || MP-WEIXIN || H5
		// #ifdef APP-PLUS
		this.playVideo()
		uni.$on('drawer-page', (data) => {
			that.clickNum(data)
		})
		// #endif
		// #ifdef H5
		this.appliance = 1

		// #endif

	},


	onShow() {

	},
	components: {
		loading,
		cLabel,
		QSpicker,
		fromItem,


	},

	onUnload() {
		uni.$off('drawer-page')
	},
	onNavigationBarButtonTap(e) {


		var that = this

		if (e.index === 0) {
			console.log(e)
			this.$refs['QS_Picekr_custom2_1'].show();

		}

	},
	methods: {

		showQSPicker(res) {
			console.log('8888');
			console.log(`pickerId为${res.pickerId},类型为${res.type}的QS-picker显示了`);
		},
		hideQSPicker(res) {
			console.log('9999');
			// console.log(`pickerId为${res.pickerId},类型为${res.type}的QS-picker隐藏了了`);
		},
		confirm(res) {
			this.layerNum = res.data[0].value;
			this.codeNum = res.data[1].value;
			// console.log('0000', res.data[0].value, res.data[1].value);
			this.geterweimaInfo(3)
			// console.log('获取了用户选择----->' + JSON.stringify(res));
		},
		geterweimaInfo(num) {

			var that = this
			if (num == 0) return false;
			var url = encodeURIComponent(window.location.href.split("#")[0]);
			this.$http.checkToken({
				url: url
			}).then(res => {
				let data = res.data;
				console.log(data)

				wx.config({
					debug: false,
					appId: data.appId,
					timestamp: data.timestamp,
					nonceStr: data.nonceStr,
					signature: data.signature,
					jsApiList: ["checkJsApi", "openLocation", "scanQRCode"],
					success(res) {
						console.log('nihao', res)
					}
				})
				wx.ready(function() {
					wx.checkJsApi({ //判断当前客户端版本是否支持指定JS接口
						jsApiList: ['scanQRCode'],
						success: function(
							res
						) { // 以键值对的形式返回，可用true，不可用false。如：{"checkResult":{"scanQRCode":true},"errMsg":"checkJsApi:ok"}
							if (res.checkResult.scanQRCode != true) {

								// uni.showToast({
								// 	title: "抱歉，当前客户端版本不支持扫一扫",
								// 	icon: 'none',
								// 	duration: 2000
								// });
							} else {
								wx.scanQRCode({
									needResult: 1, // 默认为0，扫描结果由微信处理，1则直接返回扫描结果，
									scanType: ["qrCode",
										"barCode"
									], // 可以指定扫二维码还是一维码，默认二者都有
									success: function(res) {
										var result = res
											.resultStr; // 当needResult 为 1 时，扫码返回的结果
										// uni.showToast({
										// 	title: "微信调用扫一扫接口成功" + JSON.stringify(result),
										// 	icon: 'none',
										// 	duration: 20000
										// });
										that.formData.itemsList.map(item => {
											if (item.fSampleitems ==
												result) {
												item.fLayername = that
													.layerNum;
												item.fNumname = that
													.codeNum;
												item.fWarehousingitems =
													result
												item.status = 1 //正在入库
											}
										})
										that.timeId = setTimeout(() => {
											num--
											that.geterweimaInfo(
												num--
											)
										}, 1500)
									
									},
									error: function(res) {
										console.log(res)
										if (res.errMsg.indexOf(
												'function_not_exist') > 0) {

											// uni.showToast({
											// 	title: "版本过低请升级",
											// 	icon: 'none',
											// 	duration: 2000
											// });
										}
									}


								})
							}
						},
						fail: function(res) { //检测getNetworkType该功能失败时处理
							alert('checkJsApi error');
						}
					}); //wx.checkJsApi结束




				})
				wx.error(function(res) {
					uni.showToast({
						title: "微信调用扫一扫接口报错" + JSON.stringify(res),
						icon: 'none',
						duration: 2000
					});
					// config信息验证失败会执行error函数，如签名过期导致验证失败，具体错误信息可以打开config的debug模式查看，也可以在返回的res参数中查看，对于SPA可以在这里更新签名。
				});



			})
		},
		bindMultiPickerColumnChange: function(e) {
			this.multiIndex[e.detail.column] = e.detail.value
			if (e.detail.column == 0) {
				this.positionData[1] = this.postionArr[e.detail.value].curingList
				// this.positionData[2] = this.postionArr[e.detail.value].curingList[0].layerList
				this.multiIndex.splice(1, 1, 0)
				this.multiIndex.splice(2, 1, 0)
			}
			//  else if (e.detail.column == 1) {
			// 	this.positionData[2] = this.positionData[1][e.detail.value].layerList
			// 	this.multiIndex.splice(2, 1, 0)
			// }

			this.formData.fPosition = this.postionArr[this.multiIndex[0]].fCuringno + '-' + this.postionArr[this
				.multiIndex[0]].curingList[this.multiIndex[1]].fCuringno
			// 	+ '-' + this.postionArr[this
			// 	.multiIndex[0]]
			// .curingList[this.multiIndex[1]].layerList[this.multiIndex[2]].fCuringno
			this.fWorktypeindex = 0;
			this.$forceUpdate()
		},

		submit() {
			var that = this
			this.Inv = 1

		},
		unsubmit() {
			var that = this
			this.Inv = 0
		},


		clickNum(data) {
			this.saveData(data);
		},


		getNewData() {
			var that = this;
		},
		// 详情
		getDetailData(id) {
			this.getDate();
			var that = this;
			that.fileList = [];
			var datas = {
				fId: id,
			}
			that.$http.getSampleConserveByFid(datas).then(res => {
				if (res.errcode == 0) {
					that.totalData = res.data.yjQmpYpCheckSampleconserve;
					res.data.yjQmpYpCheckSampleconserve.itemsList.map(item => {
						if (item.fWarehousingitems != '') {
							item['status'] = 0 //已入库
						}
					})
					if (that.totalData.fPosition == '' || that.totalData.fPosition == null) {
						that.fWorktypeindex = -1
					} else {

						that.fWorktypeindex = 0

						that.formData.fWarehousingdates = that.totalData.fWarehousingdates //入库日期
						that.formData.fPosition = that.totalData.fPosition //存放位置
						that.formData.fPositionid = that.totalData.fPositionid //存放位置id
						that.formData.itemsList = that.totalData.itemsList
					}

					that.formData.itemsList = res.data.yjQmpYpCheckSampleconserve.itemsList
					that.page = true
					console.log(that.formData)

				} else {
					uni.showToast({
						title: "请求失败",
						icon: 'none',
						duration: 2000
					});
				}

			}, (err) => {
				uni.showToast({
					title: "接口报错",
					icon: 'none',
					duration: 2000
				});
			})

		},

		// 下拉框
		selectorFun(e) {
			console.log("下拉选择", e);

		},
		// 获取存放位置
		getPostion(id) {
			this.positionData = [];
			var data = {
				page: 1,
				pageSize: 800,
				fCompanyid: id, //單位id
				keyWord: "" //养护室编号或名称
			};


			this.$http.getCuringlocationList(data)
				.then((res) => {
					if (res.errcode == 0) {
						console.log(11111, res.data.pageInfo.list);
						var arr = []
						var data = res.data.pageInfo.list;
						this.postionArr = res.data.pageInfo.list;
						this.positionData = [res.data.pageInfo.list, res.data.pageInfo.list[0].curingList
							// , res.data
							// 	.pageInfo.list[0].curingList[0].layerList
						];
						this.formData.fPosition = data[0].fCuringno + '-' + data[0].curingList[0].fCuringno
						// + '-' +
						// 	data[0].curingList[0].layerList[0].fCuringno

						this.fWorktypeindex = 0;
						this.formData.fPositionid = data[0].curingList[0].fCuringno.fId
					} else {
						uni.showToast({
							title: res.msg,
							icon: 'none',
							duration: 2000
						});
						// this.$refs.loading.close();
					}
				}, (err) => {
					uni.showToast({
						title: err.msg,
						icon: 'none',
						duration: 2000
					});
					// this.$refs.loading.close();
				});
		},
		layer(e, index) {
			console.log(e.target.value, index);
			this.formData.itemsList[(index - 0) + 1].fLayername = e.target.value;
			this.formData.itemsList[(index - 0) + 2].fLayername = e.target.value;
			// this.formData.itemsList.map((item,num)=>{
			// 	if(index==num){

			// 	}
			// })
		},
		code(e, index) {

			this.formData.itemsList[(index - 0) + 1].fNumname = e.target.value;
			this.formData.itemsList[(index - 0) + 2].fNumname = e.target.value;
		},
		// 判断必填项
		saveData(data) {
			// console.log(1111, this.postionArr, this.multiIndex);
			this.formData.fPosition = this.postionArr[this.multiIndex[0]].fCuringno + '-' + this.postionArr[this
				.multiIndex[0]].curingList[this.multiIndex[1]].fCuringno
			// + '-' + this.postionArr[this.multiIndex[0]]
			// .curingList[this.multiIndex[1]].layerList[this.multiIndex[2]].fCuringno
			this.formData.fPositionid = this.postionArr[this.multiIndex[0]].curingList[this.multiIndex[1]].fId;
			// .layerList[
			// 	this.multiIndex[2]].fId //存放位置id
			// data[0].curingList[0].fCuringno.fId
			var that = this;

			that.formData.isSubmit = data.toString()
			
			var num = 0
			this.formData.itemsList.map(item => {
				if (item.fWarehousingitems == "") {
					num++
					uni.showToast({
						title: "请将试件全部入库",
						icon: 'none',
						duration: 2000
					});
					return false
				}
			})
		

				if (num == 0) {
					that.$refs.loading.open();

					console.log(num)

					this.$http.saveSampleConserve(this.formData).then(res => {
						if (res.errcode == 0) {
							// that.$refs.loading.close();
							that.page = false
							if (!that.click) {
								setTimeout(() => {
									if (data == 0) {
										uni.showToast({
											title: "保存成功",
											icon: 'none',
											duration: 2000
										});
									} else if (data == 1) {
										uni.showToast({
											title: "提交成功",
											icon: 'none',
											duration: 2000
										});
									}
									uni.redirectTo({
										url: "/pages/apply/intelligentMaintenanceManage/sampleStorage/testing/index?Inv=" +
											data,
										success: () => {
											that.clickN = 1
											that.click = false
											that.$refs.loading.close();
											// #ifdef H5
											setTimeout(() => {
												that.click = false
											}, 100)
											// #endif
										}
									})
								}, 1500)
							}

							that.click = true

						} else {
							console.log("接口报错")
							that.$refs.loading.close();
							that.click = true
						}
					}, (err) => {
						that.$refs.loading.close();
						that.click = true
						uni.showToast({
							title: "请求失败",
							icon: 'none',
							duration: 2000
						});
					})
				}

			
		},


		// 原生窗体按钮显示
		playVideo() {
			let subNVue = uni.getSubNVueById('safe_notice')
			subNVue.show()
		},

		// 获取当前日期
		getDate() {
			var time = new Date();
			const y = time.getFullYear();
			const m = (time.getMonth() + 1 + "").padStart(2, "0");
			const d = (time.getDate() + "").padStart(2, "0");
			const hh = (time.getHours() + "").padStart(2, "0");
			const mm = (time.getMinutes() + "").padStart(2, "0");
			const ss = (time.getSeconds() + "").padStart(2, "0");

			var timer = `${y}-${m}-${d} ${hh}:${mm}`;
			var timing = `${y}-${m}-${d}`;

			this.formData.fWarehousingdates = `${y}-${m}-${d}`;

		},
		callback() {}
	}
}
